package com.zsh.wanandroid.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.dxq.basic_ui_library.manager.RecyclerViewLinearLayoutManager;
import com.kingja.loadsir.core.LoadSir;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.api.ApiService;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.base.BasePagerFragment;
import com.zsh.wanandroid.bean.BaseResult;
import com.zsh.wanandroid.bean.KnowledgeChildren;
import com.zsh.wanandroid.bean.KnowledgeTree;
import com.zsh.wanandroid.http.ApiRequest;
import com.zsh.wanandroid.ui.activity.SystemListActivity;
import com.zsh.wanandroid.ui.adapter.SystemAdapter;
import com.zsh.wanandroid.utils.UIUtils;

import java.io.Serializable;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zsh
 * @date 2020/3/19.
 * 功能：
 */

public class SystemPager1Fragment extends BasePagerFragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private SystemAdapter mAdapter;

    private final static int DEFAULT_PAGE = 1;//默认加载第1页

    public static SystemPager1Fragment newInstance(Bundle bundle) {
        SystemPager1Fragment fragment = new SystemPager1Fragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (null != arguments) {
        }
    }

    @Override
    protected int onCreateFragmentView() {
        return R.layout.fragment_common;
    }

    @Override
    protected void initView(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        //recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setLayoutManager(new RecyclerViewLinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));//添加分割线
        mAdapter = new SystemAdapter(R.layout.system_sys);
        recyclerView.setAdapter(mAdapter);

        initRefreshEvent();

        //item点击事件
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<KnowledgeTree<KnowledgeChildren>> data = adapter.getData();
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", (Serializable) data.get(position));
                BaseActivity activity = (BaseActivity) getActivity();
                activity.startActivity(SystemListActivity.class, bundle);
            }
        });
    }

    private void initRefreshEvent() {
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = DEFAULT_PAGE;
                loadNet();
            }
        });
    }

    @Override
    protected void loadNet() {
        //获取体系数据
        ApiService apiService = ApiRequest.getInstance(getContext()).create(ApiService.class);
        apiService.getKnowledgeTree().enqueue(new Callback<BaseResult<List<KnowledgeTree<KnowledgeChildren>>>>() {
            @Override
            public void onResponse(Call<BaseResult<List<KnowledgeTree<KnowledgeChildren>>>> call,
                                   Response<BaseResult<List<KnowledgeTree<KnowledgeChildren>>>> response) {
                swipeRefreshLayout.setRefreshing(false);
                BaseResult<List<KnowledgeTree<KnowledgeChildren>>> body = response.body();
                if (null == body) {
                    mBaseLoadService.showCallback(EmptyCallback.class);
                    return;
                }
                List<KnowledgeTree<KnowledgeChildren>> data = body.getData();
                if (null != data && data.size() > 0) {
                    mBaseLoadService.showSuccess();
                    mAdapter.replaceData(data);
                } else {
                    mBaseLoadService.showCallback(EmptyCallback.class);
                }
            }

            @Override
            public void onFailure(Call<BaseResult<List<KnowledgeTree<KnowledgeChildren>>>> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                mBaseLoadService.showCallback(ErrorCallback.class);
            }
        });
    }

    @Override
    protected void onNetReload(View v) {
        mBaseLoadService.showCallback(LoadingCallback.class);
        loadNet();
    }
}
